// assignInstall.js
var app = getApp()
var upload = ''
import { formatOrder, formatOrderNo, formatMoney } from "../../utils/util.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",
    installList: null,
    monyStars: [],
    orderId:null,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
    upload = getCurrentPages()[3]
  },
  onUnload: function () {
    upload.installObj()
    upload.setData({
      installData: this.data.assignObj
    })
  },
  //安装师傅搜索
  search: function (e) {
    console.log(e)
    this.data.installList = null;
    let self = this
    let keyword = e.detail.value
    if (e.detail.value == "" || e.detail.value==null){
      console.log("jinlaile")
      self.setData({
        installList: null
      })
    }
    self.setData({ inputVal: keyword })
    app.query('web/users/fetch_master', { keyword: keyword, page: 1, limit: 1 }).then(res => {
      if (res.code == 200) {
        console.log(res)
        if (res.users.length != 0) {
          let orders = formatOrder(res.users)
          orders[0].raty = formatMoney(orders[0].raty)
          self.setData({
            installList: orders[0]
          })
          self.stars(self.data.installList.raty)
          console.log(this.data.installList)    
        }else{
          self.setData({
            installList: null
          })
        }
      }
    })
  },
  //星星判断
  stars: function (startNum) {
    let self = this
    let starsList = []
    let imgObj = { url: "../img/me/order_star.png" }
    if (startNum != null) {
      startNum = (parseInt(startNum / 1))
      switch (startNum) {
        case 1: starsList.push(imgObj); break;
        case 2: starsList.push(imgObj, imgObj); break;
        case 3: starsList.push(imgObj, imgObj, imgObj); break;
        case 4: starsList.push(imgObj, imgObj, imgObj, imgObj); break;
        case 5: starsList.push(imgObj, imgObj, imgObj, imgObj, imgObj); break;
      }
      this.setData({
        monyStars: starsList
      })
    }
  },
  //指派
  goback: function () {
    wx.removeStorageSync('facilitator')
    if (this.data.installList){
      console.log(this.data.installList)
      wx.setStorageSync('facilitator', this.data.installList)
      app.goBack()
      // app.query("web/orders/" + this.data.orderId, assignObj, 'put').then(res => {
      //   if (res.code === 200) {
      //     console.log(res)
      //     app.goBack()
      //   }
      // })
    }else{
    }   
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: null,
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: null
    });
  },
  inputTyping: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputVal: e.detail.value
    });

  },
})